#include "MoveGun.h"
#include "SpriteMaker.h"
MoveGun::MoveGun(b2World *_World) :GunObject(_World)
{
}

MoveGun::~MoveGun()
{
}

void MoveGun::init(SpriteBatchNode* _Spritebatch, SpriteFrameCache* _Cache, Sprite *_Parent)
{
	mId = OBJECT_GUN;
	mIsAction = true;
	mIsCollision = false;

	mBody = new GunBody(mWorld,b2_staticBody);
	mBody->mColBody->SetUserData(this);
	mSprite = SpriteMaker::makeSprite(_Spritebatch, "gun_01.png");
	mAnimation = SpriteMaker::makeAnimation(_Cache, "gun", 1, 4, 1.f);
	mSprite->runAction(mAnimation);
	mSprite->setScaleX(0.5f);
	setRotation(-90);

}

void MoveGun::update(float dt)
{
	GunObject::update(dt);
}

void MoveGun::draw()
{
	GunObject::draw();
}

void MoveGun::destroy()
{
	GunObject::destroy();
}

void MoveGun::onCollisionWith(BaseObject *_Object)
{
	switch (_Object->mId)
	{
	case OBJECT_BULLET:
		if (mIsCollision == false)
		{
			mIsAction = true;
			mIsCollision = true;
			makeJointWithBullet(_Object);

			auto actionBy = RotateBy::create(2, 180);
			auto sequence = Sequence::create(actionBy, CallFunc::create([_Object, this](){
				mAction = RepeatForever::create(
					Sequence::create(
					MoveTo::create(2, Vec2(mSprite->getPositionX(), 400)),
					MoveTo::create(2, Vec2(mSprite->getPositionX(), 0)),
					nullptr));
				mSprite->runAction(mAction);

			}), NULL);
			this->mSprite->runAction(sequence);
		}
		break;

	}
}

void MoveGun::initAction()
{
	mAction = RepeatForever::create(
		Sequence::create(
		MoveTo::create(2, Vec2(mSprite->getPositionX(), 400)),
		MoveTo::create(2, Vec2(mSprite->getPositionX(), 0)),
		nullptr));
	mSprite->runAction(mAction);
}
